//
//  EGOViewController.m
//  UniverseProject
//
//  Created by king on 2021/6/23.
//  Copyright © 2021 Cloud2209. All rights reserved.
//

#import <EGOCache.h>
#import "EGOViewController.h"

@interface EGOViewController ()
@property (nonatomic, weak)UITextField *tf;
@property (nonatomic, weak)UIButton *saveBtn;
@property (strong,nonatomic) NSMutableArray *strArray;
@end

@implementation EGOViewController

- (void)viewDidLoad {
    [super viewDidLoad];
     
    
    UITextField *tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, K_SCREEN_WIDTH, 44)];
    [self.view addSubview:tf];
    tf.enablesReturnKeyAutomatically = YES;
    tf.returnKeyType = UIReturnKeyDone;
    tf.jk_identify = @"weg";
    
//    @weakify(tf);
    kWeakSelf(tf);
    tf.jk_shouldReturnBlock = ^BOOL(UITextField *textField) {
        [weaktf jk_synchronize];
        return true;
    };
    
    
    self.tf = tf;
    
    
    UIButton *saveBtn = [UIButton buttonWithTitle:@"保存" titleColor:LGCOLOR(@"000000") font:LGFONT(14) target:self action:@selector(save)];
    saveBtn.frame = CGRectMake(15, tf.bottom + 15, K_SCREEN_WIDTH-30, 44);
    saveBtn.backgroundColor = [UIColor redColor];
    [self.view addSubview:saveBtn];
    self.saveBtn = saveBtn;
    
    
    UIButton *readBtn = [UIButton buttonWithTitle:@"读取" titleColor:LGCOLOR(@"000000") font:LGFONT(14) target:self action:@selector(read)];
    readBtn.frame = CGRectMake(15, saveBtn.bottom + 15, K_SCREEN_WIDTH-30, 44);
    [self.view addSubview:readBtn];
    
    [self read];
}

- (void)save{
//    EGOCache *cache = [EGOCache globalCache];
////    [cache setString:self.tf.text forKey:@"EGOSave"];
//    [self.strArray addObject:self.tf.text];
//    [cache setObject:self.strArray forKey:@"EGOSave"];
    [self.saveBtn jk_showIndicator];
}

- (void)read{
//    EGOCache *cache = [EGOCache globalCache];
//    NSMutableArray *array = [cache objectForKey:@"EGOSave"];
//    if (!array) {
//        NSLog(@"空数组");
//       array = [NSMutableArray array];
//    }
//    self.strArray = array;
//    int random = [self getRandomNumber:0 to:array.count-1];
//    NSLog(@"%ld,%d",array.count,random);
//    self.tf.text = array[random];
    [self.tf jk_showHistory];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.tf endEditing:YES];
}



-(int)getRandomNumber:(int)from to:(int)to
{
    return (int)(from + (arc4random() % (to - from + 1)));
}

@end
